package com.zhia.demo.utils;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author DONG
 * @create 2023/11/5 11:04
 */
public class Utils {
    /**
     * 格式化需要显示的时间
     *
     * @param timestamp: 创建评论的时间
     */
    public static String formattingTime(Timestamp timestamp) {
        LocalDateTime time = timestamp.toLocalDateTime();
        LocalDateTime now = LocalDateTime.now();
        if (time.getYear() == now.getYear()) {
            long l = now.toLocalDate().toEpochDay() - time.toLocalDate().toEpochDay();
            if (l == 0) {
                if (now.getDayOfMonth() == time.getDayOfMonth()) {
                    if (now.getHour() == time.getHour()) {
                        if (now.getMinute() == time.getMinute()) {
                            return "刚刚";
                        } else {
                            return now.getMinute() - time.getMinute() + "分钟前";
                        }
                    } else {
                        return now.getHour() - time.getHour() + "小时前";
                    }
                } else {
                    return "昨天" + time.getHour() + ":" + time.getMinute();
                }
            } else if (l == 1) {
                return "昨天" + time.getHour() + ":" + time.getMinute();
            } else if (l < 7) {
                return l + "天之前";
            } else if (l < 14) {
                return "一周前";
            } else {
                return time.format(DateTimeFormatter.ofPattern("MM-dd"));
            }
        } else {
            return time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
    }
}
